var app = getApp()
Page({
    data: {
        userInfo: "",
    },
    onLoad(options) {
        setTimeout(()=>{
            this.direct();
        }, 2000)
         //数字动画部分
        var odometer = this.selectComponent('#odometer');
        var rnd = that.getRandom(1, 10);
        setTimeout(function() {
            odometer.update(rnd);
        }, 400);
        setInterval(function() {
            rnd = that.getRandom(1, 10);
            odometer.update(rnd);
        }, 2800);
    },
    getRandom: function(min, max) {
        return Math.round(Math.random() * (max - min) + min);
    },
    onShow() {
        if (this.istokenTimeOut()) {
            this.login();
        } else {
            console.log("重定向")
            wx.switchTab({url: '/pages/posts/post'})
        }
    },
    //判断缓存是否超时
    istokenTimeOut() {
        //判断token是否过期
        var expiration = wx.getStorageSync("token_expiration");//拿到过期时间
        var timestamp = Date.parse(new Date());//拿到现在时间
        //进行时间比较
        if (expiration == null || (expiration < timestamp)) {//过期了，清空缓存，跳转到登录
            wx.clearStorageSync();//清空缓存
            return true
        } else {
            return false
        }
    },
    // 调用微信登录接口
    login(auth) {
        wx.showLoading({
            title: '正在授权中'
        });
        let that = this;
        //调用接口获取登录凭证（code）
        wx.login({
            success(res) {
                wx.request({
                    url: app.globalData.apiBase + '/blog/loginSubmit',
                    data: {
                        code: res.code,
                        nickname: app.globalData.userInfo == null ? '' : app.globalData.userInfo.nickName,
                        avatar: app.globalData.userInfo == null ? '' : app.globalData.userInfo.avatarUrl
                    },
                    method: 'POST',
                    header: {
                        'content-type': 'application/json',
                        'clientType': 'WX',
                        'appId': 'wx56561420351f2442'
                    },
                    success(res) {
                        if (res.data.respCode === '0000') {
                            //手动设置过期token过期时间（因为小程序缓存没有过期时间）
                            //存一个过期时间
                            var timestamp = Date.parse(new Date());
                            var expiration = timestamp + 2592000000;//2592000秒（一个月）
                            wx.setStorageSync("token_expiration", expiration);
                            // console.log("设置的超时时间"+expiration)
                            app.globalData.userId = res.data.respResult.userId;
                            app.globalData.userName = res.data.respResult.userName;
                            wx.setStorageSync("token", res.data.respResult.token);
                            if (auth == 'auth') {
                                that.direct();
                            }
                        } else {
                            that.setData({
                                userInfo: ''
                            })
                            wx.showToast({
                                title: '登陆失败了',
                                icon: 'none',
                                duration: 1500
                            })
                        }
                    }
                })
            }
        });
        wx.hideLoading();
    },
    direct() {
        let url = "/pages/posts/post";
        wx.switchTab({
            url: url
        });
    }
});
